<?php
require_once('Extension/Controller/Plugin/Abstract.php');

class Extension_Controller_Plugin_Web_Translate extends Extension_Controller_Plugin_Abstract
{
    //public function routeStartup(Zend_Controller_Request_Abstract $request) {}
    //public function routeShutdown(Zend_Controller_Request_Abstract $request) {}

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        self::initTranslate($request);
    }

    //public function preDispatch(Zend_Controller_Request_Abstract $request) {}
    //public function postDispatch(Zend_Controller_Request_Abstract $request) {}
    //public function dispatchLoopShutdown() {}

    public static function initTranslate(Zend_Controller_Request_Abstract $request)
    {
        $locale = Zend::initLocale();

        if (Zend_Registry::isRegistered('view')) {
            $view = Zend_Registry::get('view');
        } else {
            $view = Zend_Layout::getMvcInstance()->getView();
        }

        $config = Zend::initConfig('Translate');

        $moduleName = $request->getModuleName();

        $cacheTranslate = Zend::initCache('Translate', $request->getModuleName());
        if ($cacheTranslate instanceof Zend_Cache_Core) {
            Extension_Translate::setCache($cacheTranslate);
        }

        switch ($config->adapter) {
            case (Extension_Translate::AN_ARRAY):
                $lang = 'Locale/' . $locale . '/' . $request->getModuleName() . '.php';
                if (!@include($lang)) {
                    throw new Extension_Exception($config->errMessage);
                } else {
                    $translator = new Extension_Translate(Extension_Translate::AN_ARRAY, require $lang, $locale, $config->options->toArray());
                    $view->getHelper('Translate')->setTranslator($translator);
                }   
                
                break;
            default:
                break;
        }
    }


}